jmaki.loadScript(widget.widgetDir + "/carousel.js");

var CarouselWrapper = function(uuid, wrap) {

    var lastRan = -1;
    var fList = [];
    var _wrapper = this;
    

    var fmtItem = function(imgUrl, url, title) {

        var innerHTML = 
          '<a href="' + 
  		url + 
  		'"><img src="' + 
  		imgUrl +
		'" width="' +
		75 +
		'" height="' +
		75+
		'"/>' + 
  		title + 
  		'</a>';
      
        return innerHTML;
        
    }

    var loadInitialItems = function(type, args) {
        var start = args[0];
        var last = args[1]; 

        load(this, start, last);
    }


    this.initItems = function(_items) {
        fList = _items;
    }
    
    var loadNextItems = function(type, args) {	
        var start = args[0];
        var last = args[1]; 
        var alreadyCached = args[2];
        if(!alreadyCached) {
            load(this, start, last);
        }
    }

    var loadPrevItems = function(type, args) {
        var start = args[0];
        var last = args[1]; 
        var alreadyCached = args[2];
        
        if(!alreadyCached && last < fList.length -4) {
            load(this, start, last);
        }
    }     

    var load = function(c,start, last) {
        //alert("about to add items to " + c + " start=" + start + " last=" + last);
        for(var _i=start;_i<=last;_i++) {
            if (typeof fList[_i] != 'undefined' ) {
                c.addItem(_i, fmtItem(fList[_i].thumbnailURL, fList[_i].imageURL,  fList[_i].name));
            }
        }
        //alert("added items to " + c);
    }

    var handlePrevButtonState = function(type, args) {
        //alert("handlePrevButtonState type=" + type + " args=" + args);
        var enabling = args[0];
        var leftImage = args[1];
        if(enabling) {
            leftImage.className = "prev-arrow-enabled ";	
        } else {
            leftImage.className = "prev-arrow-disabled";
        }
        
    }

    this.pageLoad = function() {
       var carousel = new YAHOO.extension.Carousel(uuid, 
            {
                numVisible:        4,
                size:             fList.length,
                animationSpeed:   .25,
                scrollInc:         3,
                navMargin:         40,
                prevElementID:     uuid + "_prev-arrow",
                nextElementID:     uuid + "_next-arrow",
                loadInitHandler:   loadInitialItems,
                loadNextHandler:   loadNextItems,
                loadPrevHandler:   loadPrevItems,
                prevButtonStateHandler:   handlePrevButtonState,
                wrap: wrap
            }
        );
    }
}

var cw = new CarouselWrapper(widget.uuid);

function fCallback (flickrPhotos) {
    // get info from the JSON object
    var fi = [];
    
    for (var l=0; l < flickrPhotos.items.length; l++) {
        var itemId = "flickr_" + l;
        var description = flickrPhotos.items[l].description;            
        var start = description.indexOf("src=") + 10;
        var stop =  description.indexOf("_m.jpg");
        var imageBase = description.substring(start,stop);
        var thumbURL = imageBase + "_m.jpg";
        var imageURL = imageBase + ".jpg";
        description = "Author: " + flickrPhotos.items[l].author + " tags:" + flickrPhotos.items[l].tags;
        var price = 0;
        var name = flickrPhotos.items[l].title;
        var mitem = {id:itemId , name: name, thumbnailURL: thumbURL, imageURL: imageURL, description: description};
        fi.push(mitem);
        
    }
    // call the callback with the flickr items
    cw.initItems(fi);
    cw.pageLoad();
    jmaki.attributes.put(widget.uuid, cw);
}

// default to the tag "theKt"
var tags = "thekt";
var fl;
var wrap = false;

if (typeof widget.args != "undefined") {
     if (typeof widget.args.wrap != "undefined") {
        tags = widget.args.wrap;
    }
    if (typeof widget.args.tags != "undefined") {
        tags = widget.args.tags;
    }
    if (typeof widget.args.tags != "undefined") {
        fl = new FlickrLoader(widget.args.apikey);
    }
    if (typeof widget.args.background != "undefined") {
        document.getElementById(widget.uuid).style.background  = widget.args.background;
    }
}

if (typeof fl == "undefined") {
    fl = new FlickrLoader();
}

fl.load(tags.toLowerCase(), fCallback);